// Burner Config for Game file module
#include "burner.h"

const int configMinVersion = 0x020921;

static wchar_t* GameConfigName()
{
	// Return the path of the config file for this game
	static wchar_t szName[MAX_PATH];
	swprintf(szName, L"config\\games\\%hs.ini", BurnDrvGetTextA(DRV_NAME));
	return szName;
}

// Read in the config file for the game-specific inputs
int configGameLoad(bool overWrite)
{
	wchar_t strLine[MAX_PATH];
	int nFileVersion = 0;

	FILE* h = _wfopen(GameConfigName(), L"rt");
	if (h == NULL) {
		return 1;
	}

	if (overWrite) {
		analogSpeed = 0x0100;
		nBurnCPUSpeedAdjust = 0x0100;
		autofire.delay = autofire.defaultDelay;
	}

	// Go through each line of the config file and process inputs
	while (fgetws(strLine, sizeof(strLine), h)) {
		wchar_t *szValue;
		size_t nLen = wcslen(strLine);

		// Get rid of the linefeed at the end
		if (strLine[nLen - 1] == 10) {
			strLine[nLen - 1] = 0;
			nLen--;
		}

		szValue = labelCheck(strLine, L"version");
		if (szValue) {
			nFileVersion = wcstol(szValue, NULL, 0);
		}

		if (overWrite) {
			szValue = labelCheck(strLine, L"analog");
			if (szValue) {
				analogSpeed = wcstol(szValue, NULL, 0);
			}
			szValue = labelCheck(strLine, L"cpu");
			if (szValue) {
				nBurnCPUSpeedAdjust = wcstol(szValue, NULL, 0);
			}
			szValue = labelCheck(strLine, L"autofire-delay");
			if (szValue) {
				autofire.delay = wcstol(szValue, NULL, 0);
			}
		}

		if (configMinVersion <= nFileVersion && nFileVersion <= burnVersion) {
			szValue = labelCheck(strLine, L"input");
			if (szValue) {
				gameInpRead(szValue, overWrite);
				continue;
			}

			szValue = labelCheck(strLine, L"macro");
			if (szValue) {
				gameInpMacroRead(szValue, overWrite);
				continue;
			}

			szValue = labelCheck(strLine, L"custom");
			if (szValue) {
				gameInpCustomRead(szValue, overWrite);
				continue;
			}
		}
	}

	fclose(h);
	return 0;
}

// Write out the config file for the game-specific inputs
int configGameSave(bool save)
{
	if (!save) {
		gameInpBlank(0);
		configGameLoad(false);
	}

	FILE* h = _wfopen(GameConfigName(), L"wt");
	if (h == NULL) {
		return 1;
	}

	// Write title
	char romname[128] = "";
	strcpy(romname, BurnDrvGetTextA(DRV_NAME));
	fwprintf(h, L"// " APP_TITLE L" v%s --- Config File for %hs (%hs)\n\n",
		appBurnVer, romname, BurnDrvGetTextA(DRV_FULLNAME));

	fwprintf(h, L"// --- Miscellaneous ----------------------------------------------------------\n\n");
	// Write version number
	fwprintf(h, L"version 0x%06X\n\n", burnVersion);
	// Write speed for relative analog controls
	fwprintf(h, L"analog  0x%04X\n", analogSpeed);
	// Write CPU speed adjustment
	fwprintf(h, L"cpu     0x%04X\n", nBurnCPUSpeedAdjust);

	// Write autofire delay
	fwprintf(h, L"\n");
	fwprintf(h, L"autofire-delay %d\n", autofire.delay);

	fwprintf(h, L"\n\n");
	fwprintf(h, L"// --- Inputs -----------------------------------------------------------------\n\n");

	gameInpWrite(h);

	fclose(h);
	return 0;
}
